<?php
/**
 * 《报价管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Quote.class.php 1043 2010-04-01 08:04:38Z sunxy $
 */
require_once(dirname(__FILE__) . '/DocumentUtil.class.php');

class Document extends DocumentUtil {
	public $tbl = Constant::tbl_document;

	public $secrecy = array(
		'-' => '请选择',
		'1' => '公开',
		'2' => '一般',
		'3' => '保密',
		'4' => '机密',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '草拟文档',
		'2' => '等待审核',
		'3' => '试用文档',
		'4' => '正式文档',

		'5' => '暂停文档',

		'8' => '文档过期',
	
		'9' => '其它',
	);

	public $groupby = array(
		'-' => '请选择',
		'urgent' => '紧急程度',
		'applicant' => '业务员',
		'emp' => '报价员',
		'times' => '次数',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	public $orderby = array(
		'-' => '请选择',
		'num' => '套数',
		'urgent' => '紧急程序',
		'date' => '要求日期',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('secrecy_option', $this->secrecy);

		$category = $this->_category();
		$controller->assign_by_ref('category_option', $category);
	}
	
	function tab($controller, $request) {		
		$data = array(
			'文档管理' => array('cc'=>'document.php?do=Document.find', 'js'=>''),
			'文档信息' => array('cc'=>'document.php?do=DocumentInfo.find', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'document/find');
		return true;
	}

	function _category() {
		$key = "document.category_";
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT DISTINCT category FROM %s WHERE length(category) > 0";
		$sql = sprintf($fmt, $this->tbl);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array('-'=>'请选择');
		while ($row = $db->fetch_array($result)) {
			$ret[$row['category']] = $row['category'];
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	function _type() {
		$key = "document.type_";
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT DISTINCT type FROM %s WHERE length(type) > 0";
		$sql = sprintf($fmt, $this->tbl);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array('-'=>'请选择');
		while ($row = $db->fetch_array($result)) {
			$ret[$row['type']] = $row['type'];
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_category($controller, $request) {
		if (!empty($request['category'])) {
			$category = $request['category'];
		}

		if (!empty($request['category_s'])) {
			$category = $request['category_s'];
		}

		if (!empty($request['category_t'])) {
			$category = $request['category_t'];
		}

		if (empty($category) || $category == '-') {
			$data[] = array('code'=>'-', 'name'=>'请选择');
			echo json_encode($data);
			return false;
		}

		$key = "document.category_" . substr(md5($category), 0, 10);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				echo json_encode($ret);
				return true;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT DISTINCT type FROM %s WHERE category = '%s'";
		$sql = sprintf($fmt, $this->tbl, $category);
		// debug($sql);
		$result = $db->query($sql);
		$ret[] = array('code'=>'-', 'name'=>'请选择');
		while ($row = $db->fetch_array($result)) {
			$row['code'] = $row['type'];
			$row['name'] = $row['type'];

			$ret[] = $row;
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("3 day"));
		}

		echo json_encode($ret);
		return true;
	}

	/**
	 * 清除缓存
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_category_type($controller, $request) {

		$category = $this->_category();
		while (list($k, $v) = each($category)) {
			$key = "document.category_" . substr(md5($k), 0, 10);
			clean($key);
		}

		$key = "document.category_";
		clean($key);

		$key = "document.type_";
		clean($key);

		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}
	
	function _where($request) {
		$category_s = $request['category_s'];
		$type_s = $request['type_s'];		
		$secrecy_s = $request['secrecy_s'];

		$my = $this->_my();
		
		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($category_s) && $category_s <> '-') {
			$where .= " AND category = '" . $category_s . "'";
		}

		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}

		if (!empty($secrecy_s) && $secrecy_s <> '-') {
			$where .= " AND secrecy = '" . $secrecy_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row['num_'] = $this->_num($row['id']);
				
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'document/list');
		return true;
	}

	function _num($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE document = '%s'";
		$sql = sprintf($fmt, Constant::tbl_document_info, $id);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign_by_ref('t1', strtotime('3 month'));

		$controller->display($request, 'document/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$name = $request['name'];

		$category = $request['category'];
		$type = $request['type'];
		$secrecy = $request['secrecy'];
		$expired = $request['expired'];

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(name, category, type, secrecy, `expired`, content, emp, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $name, $category, $type, $secrecy, $expired, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$id = $db->insert_id();
		if (empty($code)) {
			$code = $this->_code($name, $id);
		}

		$fmt = "UPDATE %s SET code = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $id);
		$db->query($sql);

		$request['id_s'] = $id;
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _code($type, $id) {
		$zjm = getPinyin($type);
		$zjm = strtoupper($zjm);
			
		$ret = substr($zjm, 0, 2) . sprintf("%04d", $id);
			
		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);
		
		$controller->display($request, 'document/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$name = $request['name'];

		$category = $request['category'];
		$type = $request['type'];
		$secrecy = $request['secrecy'];
		$expired = $request['expired'];
		$content = addslashes($request['content']);

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET name = '%s', category = '%s', type = '%s', secrecy = '%s', expired = '%s', content = '%s'";
		$fmt .= ", modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $category, $type, $secrecy, $expired, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		if (empty($code)) {
			$code = $this->_code($name, $id);
		}

		$fmt = "UPDATE %s SET code = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $id);
		debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}	

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$row['secrecy_t'] = $this->secrecy[$row['secrecy']];

		$row['emp_t'] = Name::emp($row['emp']);

		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'document/view');
		return true;
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}

		$row['secrecy_t'] = $this->secrecy[$row['secrecy']];

		$row = $this->_view_t($row);

		return $row;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 2) {
			return false;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reason($controller, $request) {
		$this->pretreat($controller);

		$status = $request['status'];
		$controller->assign_by_ref('reason', $this->status[$status]);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		if ($row['status'] == '8') {
			return $this->view($controller, $request);
		}

		$row['urgent_t'] = $this->urgent[$row['urgent']];
		$row['status_t'] = $this->status[$row['status']];

		$controller->assign_by_ref('result', $row);

		require_once(dirname(__FILE__) . '/../customer/Customer.class.php');
		$customer = new Customer();
		$controller->assign_by_ref('customer', $customer->_view($row['customer']));

		$controller->display($request, 'quote/reason');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reasonPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$reason = addslashes($request['reason']);

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET reason = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $reason, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function status($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $request['emp'];
		if (empty($emp)) {
			$emp = $_SESSION['id'];
		}

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET emp = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $status, $id);
		// debug($sql);
		$db->query($sql);

		// $this->notice($id);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}


	function status_continue($controller, $request) {
		$this->pretreat($controller);

		return $this->status_restart($controller, $request);
	}

	function status_restart($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$row = $this->id($this->tbl, $id);
		if (empty($row['emp'])) {
			$status = 2;
		} else {
			$status = 3;
		}

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$fmt = "DELETE FROM %s WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-100 day'));
		$controller->assign('t2', strtotime('+1 day'));

		$num = array(
			'1' => '按项目',
			'2' => '按产品',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'document/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'applicant');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'applicant') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} elseif ($px == 'urgent') {
			$row['a'] = $this->urgent[$row['a']];
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_mold($controller, $request) {
		$q = $request['q'];
		$data = $this->_ajax_mold($q);
		echo json_encode($data);
	}

	function _ajax_mold($q) {
		$key = "ajax_mold_" . $q;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "select id, mold, applicant, emp from %s where mold like '%s'";
		$sql = sprintf($fmt, Constant::tbl_quote, $q . "%");
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['applicant_t'] = Name::emp($row['applicant']);
			$row['emp_t'] = Name::emp($row['emp']);

			$ret[] = $row;
		}

		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	function ajax_a($controller, $request) {
		$id = $_COOKIE['id_'];
		$data = array();
		if (!$this->ajax_a_($id)) {
			$data['status'] = 2;
		} else {
			$data['status'] = 1;
		}

		$controller->assign('result', json_encode($data));

		$request['tpl'] = 'json';
		$controller->display($request, 'json');
		return true;
	}

	function ajax_a_($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['applicant'] == $_SESSION['id']) {
				return true;
			}

			if ($row['emp'] == $_SESSION['id']) {
				return true;
			}
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_exist($controller, $request) {
		$mold = $request['mold'];

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE mold = '%s'";
		$sql = sprintf($fmt, $this->tbl, $mold);
		debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			echo "false";
		} else {
			echo "true";
		}

		return true;
	}

	function ajax_bj($controller, $request) {
		$my = $_SESSION['emp'];
		if ($my['dept'] == 'bj' || $this->_is_admin()) {
			$data['status'] = 1;
		} else {
			$data['status'] = 2;
		}

		echo json_encode($data);
		return true;
	}

	function _customer($mold) {
		$pos = strpos($mold, ".");

		$code = substr($mold, 0, $pos);
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE code = '%s'";
		$sql = sprintf($fmt, Constant::tbl_customer, $code);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['id'];
	}

	function ajax_reason($controller, $request) {
		$id = $_COOKIE['id_'];

		if ($this->_ajax_reason($id)) {
			$ret = array('status'=> 1);
		} else {
			$ret = array('status'=> 2);
		}

		echo json_encode($ret);
		return true;
	}

	function _ajax_reason($id) {
		if ($this->_is_admin()) {
			return true;
		}

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		if (empty($row['emp'])) {
			if ($row['applicant'] == $_SESSION['id']) {
				return true;
			}
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	function t($controller, $request) {
		$db = newdb($this->dsn);

		$s = "3708.6";
		$c = preg_split("/[\.,]+/", $s);

		debug(print_r($c, true));

		logdate($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function z($controller, $request) {
		$db = newdb($this->dsn);

		$fmt = "SELECT mold, count(mold) t FROM %s GROUP BY mold ORDER BY t DESC";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if ($row['t'] < 2) {
				continue;
			}

			$this->_update_info($row['mold']);
		}

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}

	function _update_info($mold) {
		$db = newdb($this->dsn);

		$fmt = "SELECT * FROM %s WHERE mold = '%s' ORDER BY ID DESC";
		$sql = sprintf($fmt, $this->tbl, $mold);
		echo $sql . "\n";
		$result = $db->query($sql);
		$row = $db->fetch_array($result);
		$quote = $row['id'];
		while ($row = $db->fetch_array($result)) {
			$fmt = "UPDATE %s SET quote = '%s' WHERE quote = '%s'";
			$sql = sprintf($fmt, Constant::tbl_quote_info, $quote, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
				
			$fmt = "DELETE FROM %s WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}
	}

	/**
	 * 定时运行，每小时运行一次
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function report_daily($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "html4";

		require_once 'Plan.class.php';
		$plan = new Plan();

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status > 2 AND status < 6 AND createtime > '2010-02-01'";
		$sql = sprintf($fmt, $this->tbl);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$request['id'] = $row['id'];

			$plan->report_daily($controller, $request);
		}

		logdate($controller, $request);
		return true;
	}
	
	/**
	 * 定时运行
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function tj_daily($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "html4";

		$from = $this->email;
		$to = 'sunxy@tecmold.com';

		$subject = '新增报价情况';

		$body = $this->_tj_a($controller, $request);

		$this->mail_html($from, $to, $subject, $body);

		logdate($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function _tj_a($controller, $request) {
		$this->pretreat($controller);
			
		$groupby_s = 'applicant';
		$s = $this->groupby($controller, $groupby_s);
		$df = "DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')";

		$db = newdb($this->dsn_r);
		$sql = "SELECT $s as a, COUNT($s) as c FROM " . $this->tbl . " a ";
		$where = " WHERE createtime > " . $df;

		$orderby = ' ORDER BY c DESC';

		$groupby = " GROUP BY $s";
		$sql .= $where . $groupby . $orderby;
		debug($sql);
		$result = $db->query($sql);

		$data = array();
		$count = 0;
		$xx = 0;
		$total = 0;
		while ($row = $db->fetch_array($result)) {
			$total += $row['c'];
			if ($count > 20) {
				$xx += $row['c'];
				continue;
			}

			if ($row['c'] == 0) {
				continue;
			}

			$row = $this->_name($row, $groupby_s);
			$data[] = $row;
			$count++;
		}

		if ($xx > 0) {
			$data[] = array('a'=>'其它', 'c'=>$xx);
		}

		$aa = array();
		while (list($k, $v) = each($data)) {
			$v['p'] = number_format(($v['c'] / $total) * 100, 2, '.', '') . "%";
			$aa[] = $v;
		}
			
		$a = array();
		$c = array();
		reset($data);
		while (list($k, $v) = each($data)) {
			$a[] = $v['a'];
			$c[] = $v['c'];
		}
			
		$sa = join(",", $a);
		// echo "\n";
		$sc = join(",", $c);
			
		$sa = trim($sa, ",");
			
		$controller->assign('sa', $sa);
		$controller->assign('sc', $sc);

		$controller->assign_by_ref('total', $total);

		$controller->assign_by_ref('result', $aa);

		return $controller->sunxy($request, 'tj_a');
	}
}
?>